package com.tcm.consultation.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.base.core.exception.ServiceException;
import com.base.core.pager.PageBean;
import com.base.core.pager.PageContext;
import com.base.core.utils.R;
import com.tcm.consultation.dao.mapper.ConsultationMapper;
import com.tcm.consultation.dto.ConsultationDTO;
import com.tcm.consultation.entity.Consultation;
import com.tcm.consultation.enums.BusinessCodeEnum;
import com.tcm.consultation.service.DoctorConsultationService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

@DubboService(version = "0.0.1-alpha", retries = 3, delay = -1)
public class DoctorConsultationServiceImpl implements DoctorConsultationService {

    @Autowired
    private ConsultationMapper consultationMapper;

    /**
     * 获得订单列表
     *
     * @param doctorId
     * @param start
     * @param length
     * @return
     */
    @Override
    public PageBean<ConsultationDTO> listPageConsultation(Long doctorId, Long start, Long length) {
        Long count = consultationMapper.countByDoctorId(doctorId, null);
        PageContext.setCurrentPage(start);
        PageContext.setPageSize(length);
        PageContext.setDataCount(count);
        if(count.intValue() == 0){
            return new PageBean<>(null);
        }

        List<Consultation> consultations = consultationMapper.listPageByDoctorId(doctorId,null,
                PageContext.getStart(), PageContext.getPageSize());
        List<ConsultationDTO> consultationDTOS =  consultations.stream().map(
                consultation -> {
                    ConsultationDTO consultationDTO = new ConsultationDTO();
                    BeanUtils.copyProperties(consultation, consultationDTO);
                    return consultationDTO;
                }
        ).collect(Collectors.toList());
        return new PageBean<>(consultationDTOS);
    }

    /**
     * 获得订单列表
     *
     * @param doctorId
     * @param status
     * @param start
     * @param length
     * @return
     */
    @Override
    public PageBean<ConsultationDTO> listPageConsultation(Long doctorId, Byte status, Long start, Long length) {
        Long count = consultationMapper.countByDoctorId(doctorId, status);
        PageContext.setCurrentPage(start);
        PageContext.setPageSize(length);
        PageContext.setDataCount(count);
        if(count.intValue() == 0){
            return new PageBean<>(null);
        }

        List<Consultation> consultations = consultationMapper.listPageByDoctorId(doctorId,status,
                PageContext.getStart(), PageContext.getPageSize());
        List<ConsultationDTO> consultationDTOS =  consultations.stream().map(
                consultation -> {
                    ConsultationDTO consultationDTO = new ConsultationDTO();
                    BeanUtils.copyProperties(consultation, consultationDTO);
                    return consultationDTO;
                }
        ).collect(Collectors.toList());
        return new PageBean<>(consultationDTOS);
    }

    /**
     * 获得单个信息
     *
     * @param orderId
     * @return
     */
    @Override
    public ConsultationDTO findConsultationById(Long orderId) throws ServiceException {
        Consultation consultation = consultationMapper.findById(orderId);
        if(ObjectUtil.isNull(consultation)){
            R.throwFail(BusinessCodeEnum.CONSULTATION_ID_QUERY_FAIL);
        }
        ConsultationDTO consultationDTO = new ConsultationDTO();
        BeanUtils.copyProperties(consultation, consultationDTO);
        return consultationDTO;
    }

    /**
     * 统计订单数
     *
     * @param doctorId
     * @param status
     * @return
     */
    @Override
    public Long countConsultation(Long doctorId, Byte status) {
        return consultationMapper.countByDoctorId(doctorId,status);
    }

    /**
     * 接诊改变问诊单状态为问诊中
     *
     * @param orderId
     * @return
     */
    @Override
    public int changeStatus(Long orderId) throws ServiceException{
        Consultation consultation = consultationMapper.findById(orderId);
        if(ObjectUtil.isNull(consultation)){
            R.throwFail(BusinessCodeEnum.CONSULTATION_DATA_NOT_EXISTS);
        }
        consultation = new Consultation();
        consultation.setStatus(new Byte("2"));
        consultation.setId(orderId);
        consultation.setUpdateTime(DateUtil.date());
        int rs = consultationMapper.update(consultation);
        if(rs != 1){
            R.throwFail(BusinessCodeEnum.UPDATE_FAIL);
        }
        return 1;
    }
}
